#include "EnemyField.h"

/// Constructor
EnemyField::EnemyField()
: PotentialField()
{
	float angle = -180.0f;
	for (int i = 0; i < 36; i++)
	{

		angles[i] = angle;
		angle += 10.0f;

		insertCharge(angle, 100.0f, i);
	}
}

/// Destructor
EnemyField::~EnemyField()
{

}

/**
 * Updates all the angles and charges for the EnemyField
 */
void EnemyField::update(CarState& cs)
{

	current = cs;

	for (int i = 0; i < 36; i++)
	{
		updateCharge(angles[i], cs.getOpponents(i), i);
	}
}


/**
 * @ return - returns the resulting angle calculated by the potentialfield
 */
const float EnemyField::getAngle()
{
	float angle = 0.0f;

	
	return angle;
}

const int EnemyField::detectEnemies()
{
	float length = 0.0f;
	
	int state = ENEMY_NONE;
	
	// right
	length = getLengthOfSensors(19, 35);
	
	if (length < 3200)
	{
		state |= ENEMY_RIGHT;
	}
	
	// left
	length = getLengthOfSensors(1, 17);
	
	if (length < 3200)
	{
		state |= ENEMY_LEFT;
	}

	length = getLengthOfSensors(14, 23);
	
	if (length < 2800)
	{
		state |= ENEMY_FRONT;
	}
	
	length = getLengthOfSensors(0, 5);
	
	length += getLengthOfSensors(31, 36);
	
	if (length < 2000)
	{
		state |= ENEMY_BEHIND;
	}
	
	return state;
}

const float EnemyField::getLengthOfSensors(const int start, const int end)
{
	float length = 0.0f;
	
	if (start < end)
	{
		for (int i = start; i < end; i++)
		{
			length += current.getOpponents(i);
		}
	}
	
	return length;
}